<template>
  <el-container class="admin-layout">
    <!-- 侧边栏 -->
    <el-aside width="200px" class="sidebar" v-if="authStore.isLoggedIn">
      <div class="logo">
        <h2>管理系统</h2>
      </div>
      <el-menu
        :default-active="activeMenu"
        router
        class="sidebar-menu"
        background-color="#304156"
        text-color="#bfcbd9"
        active-text-color="#409eff"
      >
        <el-menu-item 
          v-for="item in filteredMenuItems" 
          :key="item.index"
          :index="item.index"
        >
          <el-icon><component :is="item.icon" /></el-icon>
          <span>{{ item.title }}</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <!-- 主内容区 -->
    <el-container>
      <!-- 顶部导航 -->
      <el-header class="header" v-if="authStore.isLoggedIn">
        <div class="header-content">
          <div class="header-left">
            <h2>产品管理系统</h2>
          </div>
          <div class="header-right">
            <el-dropdown @command="handleCommand" trigger="click">
              <div class="user-dropdown">
                <el-avatar
                  :size="32"
                  :src="userAvatar"
                  class="user-avatar"
                >
                  <span>{{ userInitial }}</span>
                </el-avatar>
                <span class="user-info">{{ userDisplayName }}</span>
                <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">
                    <el-icon><User /></el-icon>
                    <span>个人中心</span>
                  </el-dropdown-item>
                  <el-dropdown-item divided command="logout">
                    <el-icon><SwitchButton /></el-icon>
                    <span>退出登录</span>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-header>

      <!-- 页面内容 -->
      <el-main class="main-content">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { computed, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { ElMessage } from 'element-plus'
import { HomeFilled, Goods, Document, User, ArrowDown, SwitchButton } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const authStore = useAuthStore()

const activeMenu = computed(() => route.path)

// 确保用户信息的响应式更新
const userAvatar = computed(() => {
  return authStore.user?.avatar || ''
})

const userDisplayName = computed(() => {
  return authStore.user?.nickname || authStore.user?.username || ''
})

const userInitial = computed(() => {
  return (userDisplayName.value.charAt(0) || 'U').toUpperCase()
})

// 监听用户信息变化，确保头部更新
watch(() => authStore.user, (newUser) => {
  console.log('👤 用户信息已更新:', newUser)
}, { deep: true })

// 菜单配置
const menuItems = [
  {
    index: '/',
    title: '仪表板',
    icon: HomeFilled,
    roles: ['admin']
  },
  {
    index: '/products',
    title: '产品管理',
    icon: Goods,
    roles: ['admin']
  },
  {
    index: '/orders',
    title: '订单管理',
    icon: Document,
    roles: ['admin']
  },
  {
    index: '/profile',
    title: '个人中心',
    icon: User,
    roles: ['admin']
  }
]

// 根据用户角色过滤菜单项
const filteredMenuItems = computed(() => {
  const userRole = authStore.user?.role
  if (!userRole) return []
  
  return menuItems.filter(item => {
    return !item.roles || item.roles.includes(userRole)
  })
})

const handleCommand = async (command) => {
  if (command === 'profile') {
    router.push('/profile')
  } else if (command === 'logout') {
    await handleLogout()
  }
}

const handleLogout = async () => {
  await authStore.logout()
  ElMessage.success('已退出登录')
  router.push('/login')
}
</script>

<style scoped>
.admin-layout {
  height: 100vh;
}

.sidebar {
  background: #304156;
  overflow: hidden;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #2b3a4a;
  border-bottom: 1px solid #1f2d3d;
}

.logo h2 {
  color: #fff;
  font-size: 18px;
  margin: 0;
}

.sidebar-menu {
  border: none;
  height: calc(100vh - 60px);
}

.header {
  background: #409eff;
  color: white;
  padding: 0 20px;
  display: flex;
  align-items: center;
}

.header-content {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left h2 {
  margin: 0;
  font-size: 20px;
  color: white;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 15px;
}

.user-info {
  color: white;
  font-size: 14px;
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-dropdown:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.user-avatar {
  flex-shrink: 0;
}

.dropdown-icon {
  color: white;
  font-size: 12px;
  transition: transform 0.3s;
}

.user-dropdown:hover .dropdown-icon {
  transform: rotate(180deg);
}

:deep(.el-dropdown-menu__item) {
  display: flex;
  align-items: center;
  gap: 8px;
}

.main-content {
  background: #f0f2f5;
  padding: 20px;
}
</style>


